package com.zlsoft.web.utils.permissions;

import android.app.Activity;
import android.widget.Toast;

import com.hjq.permissions.OnPermissionCallback;
import com.hjq.permissions.Permission;
import com.hjq.permissions.XXPermissions;

import java.util.List;

/**
 * @author - qqz
 * @date - 2021-12-16/10:04
 * @note -
 * ---------------------------------------------------------------------------------------------------------------------------------
 * @modified -
 * @date -
 * @note -
 */
public class PermissionsUtils {

    private static PermissionsUtils mPermissionsUtils;

    public static PermissionsUtils getInstance() {
        if (mPermissionsUtils == null) {
            mPermissionsUtils = new PermissionsUtils();
        }
        return mPermissionsUtils;
    }

    public void getPermission(Activity mContext, PermissionCallBack mCallBack) {
        XXPermissions.with(mContext)
                // 申请单个权限
                .permission(Permission.CAMERA)
                // 申请多个权限
                //.permission(Permission.Group.CALENDAR)
                // 申请读写
                .permission(Permission.WRITE_EXTERNAL_STORAGE)
                // 读取外部存储
                .permission(Permission.READ_EXTERNAL_STORAGE)
                // 申请通知栏权限
                //.permission(Permission.NOTIFICATION_SERVICE)
                // 申请系统设置权限
                //.permission(Permission.WRITE_SETTINGS)
                // 设置权限请求拦截器
                //.interceptor(new PermissionInterceptor())
                // 设置不触发错误检测机制
                //.unchecked()
                .request(new OnPermissionCallback() {

                    @Override
                    public void onGranted(List<String> permissions, boolean all) {

                        if (null != mCallBack && all) {
                            mCallBack.onGrantedState(true);
                        } else if (null != mCallBack) {
                            mCallBack.onGrantedState(false);
                        }

                        if (all) {

                        } else {
                            Toast.makeText(mContext, "您已拒绝相机权限", Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    public void onDenied(List<String> permissions, boolean never) {
                        if (never) {
                            // 如果是被永久拒绝就跳转到应用权限系统设置页面
                            XXPermissions.startPermissionActivity(mContext, permissions);
                            Toast.makeText(mContext, "您已拒绝相机权限,请前往系统设置手动获取", Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(mContext, "您已拒绝相机权限", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
    }

    PermissionCallBack mPermissionCallBack;

    public interface PermissionCallBack {
        void onGrantedState(boolean mB);
    }
}
